package com.yyc.bleem.common.utils;

import com.yyc.bleem.pojo.entity.ResponseEntity;
import org.springframework.http.HttpStatus;

/**
 * 响应前端数据工具类
 * @author yyc
 */
public class ResponseUtil {

    /**
     * 返回工具方法
     * @param data 返回的数据
     * @return ResponseEntity
     */
    public static <T> ResponseEntity<T> createResult(T data,Integer code,String message,Boolean successFlag){
        ResponseEntity<T> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(code);
        responseEntity.setMessage(message);
        responseEntity.setData(data);
        responseEntity.setSuccessFlag(successFlag);
        return responseEntity;
    }

    /**
     * 访问成功，携带数据返回
     * @param data 返回的数据
     * @return ResponseEntity
     */
    public static <T> ResponseEntity<T> createSuccessResult(T data){
        return createResult(data,HttpStatus.OK.value(), "请求成功！",true);
    }

    /**
     * 访问失败
     * @param message：自定义的错误信息
     * @return ResponseEntity
     */
    public static <T> ResponseEntity<T> createErrorResult(T data,String message){
        return createResult(data,HttpStatus.INTERNAL_SERVER_ERROR.value(), message,false);
    }
}
